package cn.chencq.moudle.proxyip.site.impl;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import cn.chencq.moudle.proxyip.entity.ProxyIp;
import cn.chencq.moudle.proxyip.site.ProxyListPageParser;

/**
 * @ClassName: XiCiDaiLiProxyListPageParser
 * @Description: 西刺代理免费代理IP(https://www.xicidaili.com/) 24小时更新一次
 *               
 * @author: chaoqun.chen
 * @date: 2019年1月30日 下午6:23:22
 */
@Service("xiCiDaiLiProxyListPageParser")
public class XiCiDaiLiProxyListPageParser implements ProxyListPageParser {

	@Override
	public List<ProxyIp> parse(String html) {
		List<ProxyIp> proxyList = new ArrayList<>();
		Document document = Jsoup.parse(html);
		Elements elements = document.select("table#ip_list tbody tr");
		for (Element element : elements) {
			if(!(element.child(0).hasClass("country") && "td".equals(element.child(0).tagName()))) {
				continue;
			}
			String ip = element.select("td:eq(1)").first().text();
			String port = element.select("td:eq(2)").first().text();
			String isAnonymous = element.select("td:eq(4)").first().text();
			String proxyType = element.select("td:eq(5)").first().text().toLowerCase();

			String anoymity = "0";
			if ("高匿".contains(isAnonymous)) {
				anoymity = "2";
			} else if ("普匿".contains(isAnonymous)) {
				anoymity = "1";
			} else {
				anoymity = "0";
			}

			proxyList.add(new ProxyIp(ip, port, proxyType, anoymity, "0"));
		}
		return proxyList;
	}

}
